package com.demo.datastructure.stack;

/**
 * @description:
 * @author: ljr
 * @time: 2021/4/27 14:00
 */
public class MyStack<T> {

    private Node<T> head = new Node<>(null,null);

    private int length;

    public void push(T value){
        Node<T> node = new Node<>(value,head);
        head = node;
        length++;
    }

    public T pop(){
        if(length == 0){
            return  null;
        }
        T value = head.value;
        head = head.next;
        length--;
        return value;
    }

    public T peek(){
        if(length == 0){
            return null;
        }
        return  head.value;
    }

    public boolean isEmpty(){
        return length == 0;
    }


    private class Node<T>{
        public Node(T value, Node<T> next) {
            this.value = value;
            this.next = next;
        }

        private final T value;

        private final Node<T> next;
    }
}
